package com.salton123.qa.kit.fileexplorer

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.salton123.qa.R
import kotlinx.android.synthetic.main.common_item_recyclerview.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.File

/**
 * @Time:2021/1/22 14:47
 * @Author:wujinsheng
 * @Description:
 */
class TextViewerActivity : AppCompatActivity() {

    companion object {
        val FILE_KEY = "file_key"
    }

    private val mAdapter by lazy { TextViewerAdapterStyle() }
    private lateinit var targetFile: File
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        targetFile = intent?.getSerializableExtra(FILE_KEY) as File
        setContentView(R.layout.common_item_recyclerview)
        recyclerView.adapter = mAdapter
        recyclerView.layoutManager = LinearLayoutManager(this)
        val reader = targetFile.bufferedReader()
        GlobalScope.launch {
            var line = reader.readLine()
            while (line != null) {
                mAdapter.addItemAndNotify(line)
                line = reader.readLine()
            }
        }
    }
}